﻿using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using PowerManager.Annotations;
using PowerManager.Properties;

namespace PowerManager
{
    public class IconNotificator : IDisposable, IMainInterface
    {
        private readonly NotifyIcon _trayIcon;
        private readonly TrayMenuOrganizer _trayOrganizer;
        public static Settings.Settings SettingsForm;

        private static Mutex M { [UsedImplicitly] get; set; }

        public static bool IsStartCopyProgramm()
        {
            bool ok;
            M = new Mutex(true, "NameProgramm", out ok);
            if (ok) return false;
            var allProcess = Process.GetProcesses();
            var currentProcess = Process.GetCurrentProcess();
            foreach (var process in allProcess)
            {
                if (currentProcess.ProcessName != process.ProcessName || currentProcess.Id == process.Id) continue;
                var hWnd = process.MainWindowHandle;
                Application.DoEvents();
                if (hWnd != IntPtr.Zero)
                {
                    return true;
                }
            }
            MessageBox.Show(Resources.DublicateMsg, Resources.DublicateTitle,
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            return true;
        }

        public IconNotificator()
        {
            SettingsForm = new Settings.Settings(this);
            _trayIcon = new NotifyIcon();
            _trayOrganizer = new TrayMenuOrganizer(this);
            CmdHandler.ErrorThrowed += ShowError;
        }

        public void Display()
        {
            // Put the icon in the system tray and allow it react to mouse clicks.			
            _trayIcon.Icon = Resources.IconPower;
            _trayIcon.Text = Resources.trayIconMessage;
            _trayIcon.BalloonTipTitle = Resources.trayIconHelloTitle;
            _trayIcon.BalloonTipText = Resources.trayIconHelloMsg;
            _trayIcon.Visible = true;
            _trayIcon.MouseClick += trayIcon_Click;

            // Attach a context menu.
            ContextMenuCreator();
            if (Properties.Settings.Default.ShowStartMessage)
                _trayIcon.ShowBalloonTip(10000);
        }

        void trayIcon_Click(object sender, EventArgs e)
        {
            var mouse = e as MouseEventArgs;
            if (mouse != null && mouse.Button == MouseButtons.Left)
            {
                _trayIcon.BalloonTipTitle = Resources.AppName;
                _trayIcon.BalloonTipText = Resources.trayIconMessage;
                _trayIcon.ShowBalloonTip(3000);
            }
            else if (mouse != null && mouse.Button == MouseButtons.Right)
            {
                ContextMenuCreator();
            }
        }

        private void ContextMenuCreator()
        {
            _trayIcon.ContextMenuStrip = _trayOrganizer.Create();
            _trayOrganizer.SchemeChanged += SchemeChanged;
        }

        private void SchemeChanged(object sender, EventArgs eventArgs)
        {
            ContextMenuCreator();
        }

        public void Dispose()
        {
            _trayIcon.Dispose();
        }

        public void OpenSettings()
        {
            if (SettingsForm == null || SettingsForm.IsDisposed)
                SettingsForm = new Settings.Settings(this);
            SettingsForm.Show();
            SettingsForm.FormClosed += (sender, args) => ContextMenuCreator();
        }

        public void ErrorThrowed(string data)
        {
            ShowError(data, Resources.ExceptionMsg);
        }

        public void ShowError(string data, string command)
        {
            _trayIcon.BalloonTipText = data;
            _trayIcon.BalloonTipTitle = command;
            _trayIcon.ShowBalloonTip(3000);
        }
    }
}
